<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/20
 * Time: 14:16
 */

namespace Fanpaiji;


use Maatwebsite\Excel\Excel;

class ExportFile
{
    public function __construct()
    {
        $this->dbConfig = array(
            'DB_CONFIG' => array(
                //数据库配置
                'DB_HOST'=>'192.168.1.22',    //服务器地址
//                'DB_NAME' => '58car_58fabu', // 数据库名
                'DB_NAME' => 'vin', // 数据库名
                'DB_USER' => 'root', // 用户名
                'DB_PWD' => '123456', // 密码
                'DB_ENCODE'=>'utf8',//编码
                'DB_PORT' => '3306', //端口
                'DB_PREFIX' => '' // 数据库表前缀
            )
        );
    }

    public function export()
    {
        header("Content-type:application/vnd.ms-excel");
        header("Content-Disposition:filename=abc.xls");

        $strexport="ID\tpath\t品牌\t厂商\t车系\t排量\t年款\t变速箱\t名称\t品牌图片\t车系图片\t车型图片\r";

        $rypM = new M('`vin_series_2018-03-19`',$this->dbConfig);

        $select = ['id','first_char','parent_id','path','value','logo','type'];
        $data = $rypM->select("type=6 order by path asc",$select);

        $str = '<table id="series_tb">
				<tr class="daohang">
					<td class="td1">ID</td>
					<td class="td2">path</td>
					<td class="td2">品牌</td>
					<td class="td3">厂商</td>
					<td class="td4">车系</td>
					<td class="td5">排量</td>
					<td class="td6">年款</td>
					<td class="td7">变速箱</td>
					<td class="td8">名称</td>
                    <td class="td11">品牌图片</td>
                    <td class="td11">车系图片</td>
					<td class="td11">车型图片</td>
				</tr>
				';


			$str .= '</table>';

        foreach ( $data as $datum) {
            $subData = $rypM->select("id in ({$datum['path']})",$select);
            $strexport .= $datum['id']."\t";
            $strexport .= "'{$datum['path']}'"."\t";
            $strexport .= $subData[0]['value']."\t";
            $strexport .= $subData[1]['value']."\t";
            $strexport .= $subData[2]['value']."\t";
            $strexport .= $subData[3]['value']."\t";
            $strexport .= $subData[4]['value']."\t";
            $strexport .= $subData[5]['value']."\t";
            $strexport .= $datum['value']."\t";
            $strexport .=$subData[0]['logo']."\t";
            $strexport .=$subData[2]['logo']."\t";
            $strexport .=$datum['logo']."\r";

//            $str .= '<tr>
//					<td class="td1">'.$datum['id'].'</td>
//					<td class="td1">'.$datum['path'].'</td>
//					<td class="td1">'.$subData[0]['value'].'</td>
//					<td class="td1">'.$subData[1]['value'].'</td>
//					<td class="td1">'.$subData[2]['value'].'</td>
//					<td class="td1">'.$subData[3]['value'].'</td>
//					<td class="td1">'.$subData[4]['value'].'</td>
//					<td class="td1">'.$subData[5]['value'].'</td>
//					<td class="td1">'.$datum['value'].'</td>
//					<td class="td1">'.$subData[0]['logo'].'</td>
//					<td class="td1">'.$subData[2]['logo'].'</td>
//					<td class="td1">'.$datum['logo'].'</td>
//
//				</tr>';

//            Tools::showData($subData);
        }

        $strexport=iconv('UTF-8',"GB2312//IGNORE",$strexport);
        exit($strexport);

        echo $str;exit;
        Tools::showData($str);

    }
}